#include<iostream>
#include<vector>
#define maxsize 100
using namespace std;
int n;
int dist[maxsize][maxsize];
vector<int> path[maxsize][maxsize];
vector<int> JoinList(int i,int k,int j)//合并路径
{
	path[i][j].clear();
	int tail = path[i][k].back();
	path[i][k].pop_back();
	vector<int>temp;
	for (int m = 0; m < (signed)path[i][k].size(); m++)
	{
		temp.push_back(path[i][k][m]);
	}
	for (int n = 0; n < (signed)path[k][j].size(); n++)
	{
		temp.push_back(path[k][j][n]);
	}
	path[i][k].push_back(tail);
	return temp;
}
void flyd()
{
	for (int i = 0; i < n; i++)//初始化dist[i][j]与path[i][j]
	{
		for (int j = 0; j < n; j++)
		{
			cin >> dist[i][j];
			if (dist[i][j] < 9999)
			{
				path[i][j].push_back(i);
				path[i][j].push_back(j);
			}
			else if(i==j&&dist[i][j]==9999)//对角线上path填一个顶点即可
			{
				path[i][j].push_back(i);
			}
		}
	}
	for (int k = 0; k < n; k++)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (dist[i][k] + dist[k][j] < dist[i][j]&&i!=j)//顶点自身无法到达
				{
					dist[i][j] = dist[i][k] + dist[k][j];
					path[i][j] = JoinList(i,k,j);
				}
			}
		}
	}
}
void printAns()
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (dist[i][j] == 9999) dist[i][j] = 0;
			cout << "from " << i << " to " << j << ": " << "dist = " << dist[i][j] << " path:";
			for (int k = 0; k < (signed)path[i][j].size();k++)
			{
				cout<<path[i][j][k]<<" ";
			}
			cout << endl;
		}
	}
}
int main()
{
	cin >> n;
	flyd();
	printAns();
	//system("pause");
	return 0;
}
